package com.imooc.controller;

import com.imooc.enums.ErrorMsg;
import com.imooc.pojo.Items;
import com.imooc.pojo.ItemsImg;
import com.imooc.pojo.ItemsParam;
import com.imooc.pojo.ItemsSpec;
import com.imooc.pojo.vo.ItemInfoVo;
import com.imooc.service.ItemService;
import com.imooc.utils.CommonJsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 逸尘风清
 * @date 2021/5/8
 * @time 15:20
 * 描述：
 */
@Api(value = "商品接口" , tags = {"商品相关接口"})
@RestController
@RequestMapping("items")
public class ItemController extends BaseController{
    @Resource
    private ItemService itemService;

    @ApiOperation(value = "商品详情" , notes = "商品详情信息")
    @GetMapping("/info/{itemId}")
    public CommonJsonResult info(
            @ApiParam(value = "商品id", required = true)
            @PathVariable String itemId){

        if (itemId == null) {
            return CommonJsonResult.errorMsg(ErrorMsg.ERROR_PARAMS);
        }
        Items item = itemService.queryItemById(itemId);
        List<ItemsImg> itemsImgList = itemService.queryItemImgList(itemId);
        ItemsParam itemsParam = itemService.queryItemsParam(itemId);
        List<ItemsSpec> itemsSpecList = itemService.queryItemsSpecList(itemId);
        ItemInfoVo itemInfoVo = new ItemInfoVo();
        itemInfoVo.setItem(item);
        itemInfoVo.setItemParams(itemsParam);
        itemInfoVo.setItemImgList(itemsImgList);
        itemInfoVo.setItemSpecList(itemsSpecList);
        return CommonJsonResult.ok(itemInfoVo);
    }

    @ApiOperation(value = "查看商品评价等级",notes = "查看商品评价等级",httpMethod = "GET")
    @GetMapping("/commentLevel")
    public CommonJsonResult commentLevel(
            @ApiParam(name = "itemId",value = "商品ID",required = true)
            @RequestParam String itemId){
        if(StringUtils.isBlank(itemId)){
            return CommonJsonResult.errorMsg(ErrorMsg.ERROR_PARAMS);
        }
        return CommonJsonResult.ok(itemService.queryCommentCounts(itemId));
    }

    @ApiOperation(value = "商品的评价分页列表",notes = "商品的评价分页列表",httpMethod = "GET")
    @GetMapping("/comments")
    public CommonJsonResult comments(
            @ApiParam(name = "itemId",value = "商品ID",required = true)
            @RequestParam String itemId,
            @ApiParam(name = "level",value = "评级的等级",required = false)
            @RequestParam Integer level,
            @ApiParam(name = "page",value = "当前页",required = true)
            @RequestParam(name = "page",defaultValue = "1") Integer page,
            @ApiParam(name = "pageSize",value = "每页显示的数量",required = true)
            @RequestParam(name = "pageSize",defaultValue = "10") Integer pageSize){
        if(StringUtils.isBlank(itemId)){
            return CommonJsonResult.errorMsg(ErrorMsg.ERROR_PARAMS);
        }
        if (page == null) {
            page = 1;
        }
        if (pageSize == null) {
            pageSize = COMMENT_PAGE_SIZE;
        }
        return CommonJsonResult.ok(itemService.queryPagedComments(itemId,level,page,pageSize));
    }

    @ApiOperation(value = "商品搜索展示分页列表",notes = "商品搜索展示分页列表",httpMethod = "GET")
    @GetMapping("/search")
    public CommonJsonResult search(
            @ApiParam(name = "keywords",value = "搜索关键词",required = false)
            @RequestParam String keywords,
            @ApiParam(name = "sort",value = "搜索排序规则",required = false)
            @RequestParam String sort,
            @ApiParam(name = "page",value = "当前页",required = true)
            @RequestParam(name = "page",defaultValue = "1") Integer page,
            @ApiParam(name = "pageSize",value = "每页显示的数量",required = true)
            @RequestParam(name = "pageSize",defaultValue = "20") Integer pageSize){

        if (page == null) {
            page = 1;
        }
        if (pageSize == null) {
            pageSize = PAGE_SIZE;
        }
        return CommonJsonResult.ok(itemService.searchItems(keywords,sort,page,pageSize));
    }

    @ApiOperation(value = "通过分类id搜索商品分页列表",notes = "通过分类id搜索商品分页列表",httpMethod = "GET")
    @GetMapping("/catItems")
    public CommonJsonResult catItems(
            @ApiParam(name = "catId",value = "搜索关键词",required = true)
            @RequestParam Integer catId,
            @ApiParam(name = "sort",value = "搜索排序规则",required = false)
            @RequestParam String sort,
            @ApiParam(name = "page",value = "当前页",required = true)
            @RequestParam(name = "page",defaultValue = "1") Integer page,
            @ApiParam(name = "pageSize",value = "每页显示的数量",required = true)
            @RequestParam(name = "pageSize",defaultValue = "20") Integer pageSize){

        if(catId == null){
            return CommonJsonResult.errorMsg(ErrorMsg.ERROR_PARAMS);
        }
        if (page == null) {
            page = 1;
        }
        if (pageSize == null) {
            pageSize = PAGE_SIZE;
        }
        return CommonJsonResult.ok(itemService.searchItemsByThirdCat(catId,sort,page,pageSize));
    }
}
